package com.avit.tv.kui.progressbar;


import com.avit.tv.kui.utils.TimeUtils;

import java.util.*;

/**
 * progressbar 的适配器
 *
 * @author like
 * @date 2020/7/9
 */
public abstract class ProgressbarAdapter<T> implements Adapter<T> {

    private float maxProgress;

    /**
     * 关键点
     */
    private List<ImportPointValue> importantPoints;

    /**
     * 与现在时长的差距时间 [单位 ：小时]
     */
    protected int dertaHour;

    /**
     * 更新内容
     */
    private  final Map<Long, Float> timeClock = new HashMap<>();

    /**
     * 获取滚动条单倍距离
     * {@hide}
     *
     * @param maxWidth
     * @return
     */
    public float initPercentMoveWidth(float maxWidth) {
        maxProgress = Math.abs(getStartTimeSec() - getEndTimeSec());
        return maxWidth / maxProgress;
    }


    /**
     * 设置最大时间差 ， 单位：小时
     *
     * @param dertaHour
     */
    public void setDertaHour(int dertaHour) {
        this.dertaHour = dertaHour;
    }



    /**
     * 获取关键点与起始点的距离的集合
     * {@hide}
     *
     * @return
     */
    public List<ImportPointValue> getFinalPointX(float percentMoveWidth) {
        if (importantPoints == null || importantPoints.isEmpty()) return new ArrayList<>();
        for (ImportPointValue pointTime : importantPoints) {
            long startDerta = pointTime.getPointStartTime() - getStartTimeSec();
            long endDerta = getEndTimeSec() - pointTime.getPointStartTime();
            if (startDerta > 0 && endDerta > 0) {
                float dertaX =  startDerta * percentMoveWidth;
                pointTime.setShowX(dertaX);
            }else
                pointTime.setShowX(-1);
        }
        return importantPoints;
    }

    /**
     * 判断是否是第一个节目点
     * @return
     */
    public boolean isShowStartText(long endTime, long startTime){
        return endTime>getStartTimeSec() && startTime < getStartTimeSec();
    }

    /**
     * 获取所有的点
     * @return
     */
    public List<ImportPointValue> getImportantPoints() {
        return importantPoints;
    }

    /**
     * 设置关键帧
     *
     * @param importantPoints
     */
    protected void setImportantPoints(List<ImportPointValue> importantPoints) {
        this.importantPoints = importantPoints;
    }

    /**
     * 获取当前距离
     * {@hide}
     *
     * @param
     * @return
     */
    public float getTotalWidth(long sec, float maxWidth) {
        long longTimeSec = getStartTimeSec();
        long dertaTime = sec - longTimeSec;
        if (dertaTime < 0) return 0;
        return dertaTime * initPercentMoveWidth(maxWidth);
    }


    /**
     * 获取刻度尺
     * {@hide}
     * @return
     */
    public Map<Long , Float> getClockWidth(float percentMoveWidth){
        timeClock.clear();
        long startTimeSec = getStartTimeSec();
        Date date = new Date(startTimeSec * 1000);
        long timeDerta = getTimeDerta(date);
        long resultTimeSec = startTimeSec+timeDerta;
        timeClock.put(resultTimeSec, timeDerta*percentMoveWidth);
        long dertaTime = TimeUtils.TimeValued.FIFTEEN_MIN *60;
        for (int i=1 ; i<16;i++){
            long resultSec = resultTimeSec + dertaTime*i;
            float moved = (timeDerta + dertaTime * i  - date.getSeconds())*percentMoveWidth;
            timeClock.put(resultSec, moved);
        }
        return timeClock;
    }

    /**
     * 比较标准时间
     * @return
     */
    private long getTimeDerta(Date date ){
        int minutes = date.getMinutes();
        final int[] fourTimes = TimeUtils.TimeValued.getValueMins();
        for (int time: fourTimes) {
            if (Math.max(minutes, time)==time) {
                return (time - minutes) * 60;
            }
        }
        return (TimeUtils.TimeValued.ONE_HOUR_MIN- minutes)*60;
    }

}
